import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.text.TextField;
import flash.geom.Rectangle;

class VectorView extends Sprite
{
  private var _v1:VectorModel;
  private var _vx:Float;
  private var _vy:Float;
  private var _startX:Float;
  private var _startY:Float;
  private var _endx:Float;
  private var _endy:Float;
  private var _rx:Float;
  private var _ry:Float;
  private var _lx:Float;
  private var _ly:Float;
  private var _dx:Float;
  private var _dy:Float;
  private var _angle:Float;
  private var _m:Float;
  private var _scaleAmount:Float;
  
  //Each vector line is Shape object
  private var _mainVector:Shape;
  private var _rn:Shape;
  private var _ln:Shape;
  private var _mainVector_Vx:Shape;
  private var _mainVector_Vy:Shape;
  
  //Strings objects, which will contain the actual text displayed
  private var _mainVector_Text:String;
  private var _rn_Text:String;
  private var _ln_Text:String;
  private var _vx_Text:String;
  private var _vy_Text:String;
  
  //RotationText, which will contain the strings used to lablel the vectors
  //RotationText is a custom class that creates and formats the vector label text
  //It also includes an embedded font
  private var _statusBox:TextField;
  private var _rn_Label:RotationText;
  private var _ln_Label:RotationText;
  private var _vx_Label:RotationText;
  private var _vy_Label:RotationText;

  private var _camera:Rectangle;
  
  public function new(vector:VectorModel, camera:Rectangle, ?scaleAmount:Float = 1) 
  {
    super();
    _camera = camera;
    this._v1 = vector;
    _v1.addEventListener(Event.CHANGE, onChange);
    
    this._scaleAmount = scaleAmount;
    
    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  }

  private function onAddedToStage(event:Event)
  {
    _mainVector = new Shape();
    addChild(_mainVector);
    
    
    addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
  }
  private function onRemovedFromStage(event:Event)
  {
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
  }
  
  //onChange  is called by the VectorModel's update method
  
  public function onChange(event:Event)
  {
    //Find the new vector coordinates
    
    _startX = _v1.a.x;
    _startY = _v1.a.y;
    _m = _v1.m;
    
    
    //Sometimes you need a vector that's a different length
    //but points in the same direction. Here's how
    //to scale a vector:
    
    if(_scaleAmount != 1)
    {
      //1. Scale the length
      
      var newMagnitude:Float = _m * _scaleAmount;
      _m = newMagnitude;
      
      //2. Scale the vector by multiplying the normalized vector with the new length
      
      _endx = _startX + (_v1.dx * _m);
      _endy = _startY + (_v1.dy * _m);
    }
    else
    {
      _endx = _v1.b.x;
      _endy = _v1.b.y;
    }
    
    _vx = _v1.vx;
    _vy = _v1.vy;
    _rx = _v1.rx;
    _ry = _v1.ry;
    _lx = _v1.lx;
    _ly = _v1.ly;
    _dx = _v1.dx;
    _dy = _v1.dy;
    _angle = _v1.angle;

    //Draw the main vector
    
    _mainVector.graphics.clear();
    _mainVector.graphics.lineStyle(1, 0x111111, 1);
    _mainVector.graphics.moveTo(_startX-_camera.x, _startY-_camera.y); 
    _mainVector.graphics.lineTo(_endx-_camera.x, _endy-_camera.y);

  }

  public function Hit()
  {
    _mainVector.graphics.clear();
    _mainVector.graphics.lineStyle(1, 0xFF1111, 1);
    _mainVector.graphics.moveTo(_startX-_camera.x, _startY-_camera.y); 
    _mainVector.graphics.lineTo(_endx-_camera.x, _endy-_camera.y);
  }
  public function UnHit()
  {
    _mainVector.graphics.clear();
    _mainVector.graphics.lineStyle(1, 0x111111, 1);
    _mainVector.graphics.moveTo(_startX-_camera.x, _startY-_camera.y); 
    _mainVector.graphics.lineTo(_endx-_camera.x, _endy-_camera.y);
  }

}